﻿#region Copyright (c) 2013-08, Olaf Kober <amarok.projects@gmail.com>
//================================================================================
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.
//================================================================================
#endregion

using System;
using System.ComponentModel;
using System.Linq;
using System.Windows;


namespace Amarok.Presentation
{
	/// <summary>
	/// This type contains various attached properties related to Dialog-specific functionality.
	/// </summary>
	public static class DialogBehavior
	{
		#region ++ Dependency Property: IsDialog ++

		/// <summary>
		/// This field identifies the attached dependency property IsDialog, which is a boolean value indicating 
		/// whether the Window should be styled as a Dialog.
		/// </summary>
		public static readonly DependencyProperty IsDialogProperty = DependencyProperty.RegisterAttached(
			"IsDialog",
			typeof(Boolean),
			typeof(DialogBehavior),
			new FrameworkPropertyMetadata(
				false,
				OnIsDialogChanged
			)
		);


		/// <summary>
		/// Gets the value of the attached property IsDialog for the given element, which is a boolean value 
		/// indicating whether the Window should be styled as a Dialog.
		/// </summary>
		[
			Description("A boolean value indicating whether the Window should be styled as a Dialog."),
			CustomPropertiesCategory,
			AttachedPropertyBrowsableForType(typeof(Window))
		]
		public static Boolean GetIsDialog(Window element)
		{
			// IMPORTANT:	To maintain parity between XAML and procedural code, 
			//				do not touch this method!
			return (Boolean)element.GetValue(IsDialogProperty);
		}

		/// <summary>
		/// Sets the value of the attached property IsDialog for the given element.
		/// </summary>
		public static void SetIsDialog(Window element, Boolean value)
		{
			// IMPORTANT:	To maintain parity between XAML and procedural code, 
			//				do not touch this method!
			element.SetValue(IsDialogProperty, value);
		}


		private static void OnIsDialogChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			Window element = d as Window;

			if (element == null)
				return;

			OnIsDialogChanged(element, (Boolean)e.OldValue, (Boolean)e.NewValue);
		}

		private static void OnIsDialogChanged(Window element, Boolean oldValue, Boolean newValue)
		{
			if (Designer.IsDesignMode)
				return;

			if (newValue == false)
				return;

			if (element.IsLoaded)
			{
				Wpf.SetupDialogWindowStyle(element);
			}
			else
			{
				element.Loaded += _HandleWindowSourceLoaded;
			}
		}

		private static void _HandleWindowSourceLoaded(Object sender, RoutedEventArgs e)
		{
			var window = e.OriginalSource as Window;

			if (window == null)
				return;

			Wpf.SetupDialogWindowStyle(window);

			window.Loaded -= _HandleWindowSourceLoaded;
		}

		#endregion

	}
}
